//
//  ViewController.m
//  核心动画代码
//
//  Created by 申元江 on 2018/7/30.
//  Copyright © 2018年 申元江. All rights reserved.
//

#import "ViewController.h"

/**
 动画组   https://blog.csdn.net/u014286994/article/details/51316941
 */
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self test];
}

-(void)test
{
    CGFloat red = arc4random() / (CGFloat) INT_MAX;
    CGFloat green = arc4random() / (CGFloat) INT_MAX;
    CGFloat blue = arc4random() / (CGFloat) INT_MAX;
    UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1];
    //绘制贝塞尔曲线
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:CGPointMake(50, 150)];
    //添加一个曲线（切线上的点）
    [bezierPath addCurveToPoint:CGPointMake(400, 150) controlPoint1:CGPointMake(170, 0) controlPoint2:CGPointMake(225, 300)];
    
    //设置曲线的layer
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = bezierPath.CGPath;
    //线条的颜色
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    //layer线条的宽度
    shapeLayer.lineWidth = 2.0f;
    //填充颜色
    shapeLayer.fillColor = nil;
    [self.view.layer addSublayer:shapeLayer];
    
    //设置colorlayer
    CALayer *colorLayer = [CALayer layer];
    colorLayer.frame = CGRectMake(0, 0, 66, 66);
    colorLayer.position = CGPointMake(50, 150);
    colorLayer.backgroundColor = color.CGColor;
    [self.view.layer addSublayer:colorLayer];
    
    //添加位置动画
    CAKeyframeAnimation *anim1 = [CAKeyframeAnimation animation];
    anim1.keyPath = @"position";
    anim1.path = bezierPath.CGPath;
    anim1.rotationMode = kCAAnimationRotateAuto;
    
    //添加比例动画
    CABasicAnimation *anim2 =[CABasicAnimation animation];
    anim2.keyPath = @"transform.scale";
    anim2.toValue = @0.1;
    
    //添加颜色动画
    CABasicAnimation *anim3 = [CABasicAnimation animation];
    anim3.keyPath = @"backgroudColor";
    anim3.toValue = (__bridge id)color.CGColor;
    
    CAAnimationGroup *group =[CAAnimationGroup animation];
    group.animations = @[anim1,anim2,anim3];
    group.duration  =2.0f;
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeBoth;
    [colorLayer addAnimation:group forKey:nil];
    
    
    
    
    
    
    //画一个view 使得他的某个脚是直角 某个脚是圆角
    UIBezierPath *path1 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 300, 150, 80) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *layer1 = [CAShapeLayer layer];
    layer1.path = path1.CGPath;
    layer1.fillColor =  [UIColor blueColor].CGColor;
    [self.view.layer addSublayer:layer1];
    
    
}




-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self test];
}


















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
